package com.fairytask.engine.components.table
{
	import com.fairytask.data.BaseData;
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.define.Filter;
	
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	/**
	 * 表格单元格 
	 * @author Navy
	 */	
	public class TableItem extends BaseComponent
	{
		private var _data	:BaseData	=	new BaseData();
		private var _text	:TextField	=	new TextField();
		private var _tf		:TextFormat	=	new TextFormat();
		private var _itemWidth	:Number	=	0;
		//显示
		private var _itemBg	:Shape	=	new Shape();
		//属性
		private var _width		:Number;
		private var _height	:Number;
		private var _labelStr	:String	=	"";
		private var _pos		:int	=	0;
		private var _isLink	:Boolean;
		
		public function TableItem(str:String,isTile:Boolean=false,_w:int=-1,strColor:String="FFFFFF")
		{
			//设置文字风格与样式
			_text.selectable	=	false;
			_text.autoSize	=	TextFieldAutoSize.CENTER;
			_text.filters = [Filter.COMMON_TEXT_FILTER];
			if(_w!=-1)
			{
				_itemWidth	=	_w;
				_text.width	=	_w;
			}
			_labelStr	=	str;
			if(isTile)
			{
				_text.textColor	=	0xFFCB4A;
			}
			else
			{
				_text.textColor	=	int("0x"+strColor);
			}
			_text.setTextFormat(_tf);
			_text.htmlText	=	str;
			_text.x	=	(_w-_text.width)/2;
			addChild(_text);
			
			initEvent();
		}
		
		/**
		 * 初始化事件 
		 */
		private function initEvent():void
		{
			_text.addEventListener(TextEvent.LINK, onItemLink);
			_text.addEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function onClick(evt:MouseEvent):void
		{
			if(_isLink)
			{
				evt.preventDefault();
				evt.stopImmediatePropagation();
			}
		}
		
		private function onItemLink(evt:TextEvent):void
		{
			_isLink = true;
			if(this.parent)
			{
				_data.HandleProperties(TableEvent.ITEM_LINK,evt.text);
				this.parent.dispatchEvent(new TableEvent(TableEvent.ITEM_LINK,pos,_data));
			}
		}
		
		/**
		 * 设置描边 
		 */
		public function setBorder(val:Array):void
		{
			_text.filters	=	val;
		}
		
		override public function get width():Number
		{
			return _width;
		}

		override public function set width(value:Number):void
		{
			_width = value;
		}

		override public function get height():Number
		{
			return _height;
		}

		override public function set height(value:Number):void
		{
			_height = value;
		}
		
		/**
		 * 字体大小
		 * @param val
		 * 
		 */
		public function set txtSize(val:uint):void
		{
			_tf.size	=	val;
			_text.setTextFormat(_tf);
		}
		
		/**
		 * 字体颜色 
		 * @param val
		 * 
		 */
		public function set txtColor(val:uint):void
		{
			_text.textColor	=	val;
		}

		public function get labelStr():String
		{
			return _labelStr;
		}

		public function set labelStr(value:String):void
		{
			_labelStr = value;
			_text.htmlText	=	_labelStr;
		}

		public function get itemWidth():Number
		{
			return _itemWidth;
		}

		public function set itemWidth(value:Number):void
		{
			_itemWidth = value;
		}

		public function get pos():int
		{
			return _pos;
		}

		public function set pos(value:int):void
		{
			_pos = value;
		}


	}
}